package com.google.guava.concurrent;

import com.google.common.util.concurrent.Monitor;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.Thread.currentThread;

/**
 * @author fuc
 * @create 2020-10-08
 */
public class MonitorExample {

  public static void main(String[] args) {
    final MonitorGuard lc = new MonitorGuard();
    final AtomicInteger COUNTER = new AtomicInteger(0);

    for (int i = 0; i < 3; i++) {
      new Thread(
              () -> {
                for (; ; ) {
                  try {
                    int data = COUNTER.getAndIncrement();
                    System.out.println(currentThread() + " offer " + data);
                    lc.offer(data);
                    TimeUnit.MILLISECONDS.sleep(2);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                }
              })
          .start();
    }

    for (int i = 0; i < 3; i++) {
      new Thread(
              () -> {
                for (; ; ) {
                  try {
                    int take = lc.take();
                    System.out.println(currentThread() + " take " + take);
                    TimeUnit.MILLISECONDS.sleep(1);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                }
              })
          .start();
    }
  }

  static class MonitorGuard {
    private final LinkedList<Integer> queue = new LinkedList<Integer>();
    private final int MAX = 10;
    private final Monitor monitor = new Monitor();

    private final Monitor.Guard CAN_OFFER = monitor.newGuard(() -> queue.size() < MAX);

    private final Monitor.Guard CAN_TAKE = monitor.newGuard(() -> !queue.isEmpty());

    public void offer(int value) {
      try {
        monitor.enterWhen(CAN_OFFER);
        queue.addLast(value);
      } catch (InterruptedException e) {
        e.printStackTrace();
      } finally {
        monitor.leave();
      }
    }

    public int take() {
      Integer value = null;
      try {
        monitor.enterWhen(CAN_TAKE);
        return queue.removeFirst();
      } catch (InterruptedException e) {
        e.printStackTrace();
      } finally {
        monitor.leave();
      }
      return value;
    }
  }

  static class LockCondition {
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition FULL_CONDITION = lock.newCondition();
    private final Condition EMPTY_CONDITION = lock.newCondition();

    private final LinkedList<Integer> queue = new LinkedList<Integer>();
    private final int MAX = 10;

    public void offer(int value) {
      try {
        lock.lock();
        while (queue.size() >= MAX) {
          FULL_CONDITION.wait();
        }
        queue.addLast(value);
        EMPTY_CONDITION.signalAll();
      } catch (InterruptedException e) {
        e.printStackTrace();
      } finally {
        lock.unlock();
      }
    }

    public int take() {
      Integer value = null;
      try {
        lock.lock();
        while (queue.isEmpty()) {
          EMPTY_CONDITION.wait();
        }
        value = queue.removeFirst();
        FULL_CONDITION.signalAll();
        queue.notifyAll();
      } catch (InterruptedException e) {
        e.printStackTrace();
      } finally {
        lock.unlock();
      }

      return value;
    }
  }

  static class Synchronized {
    private final LinkedList<Integer> queue = new LinkedList<Integer>();
    private final int MAX = 10;

    public void offer(int value) {
      synchronized (queue) {
        while (queue.size() >= MAX) {
          try {
            queue.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        queue.addLast(value);
        queue.notifyAll();
      }
    }

    public int take() {
      synchronized (queue) {
        while (queue.isEmpty()) {
          try {
            queue.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        Integer value = queue.removeFirst();
        queue.notifyAll();
        return value;
      }
    }
  }
}
